package flower.com.a600_xm.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.Collection;
import java.util.List;

import de.greenrobot.event.EventBus;
import flower.com.a600_xm.R;
import flower.com.a600_xm.base.MyApplication;
import flower.com.a600_xm.bean.MyEvent;
import flower.com.a600_xm.db.dbbean.Life_Shopping;
import flower.com.a600_xm.utils.UiUtils;

/**
 * Created by Sachi Reddy on 3/29/2015.
 */
public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ContactViewHolder> {

    private List<Life_Shopping> contactList;

    public ContactAdapter(List<Life_Shopping> contactList) {
        this.contactList = contactList;
    }

    /**
     * 集合方式添加数据
     *
     * @param collection 集合
     */
    public boolean addItem(List<Life_Shopping> collection) {
        boolean b = contactList.addAll(collection);
        notifyDataSetChanged();
        return b;

    }

    public boolean addItem(Life_Shopping collection) {
        boolean b = contactList.add(collection);
        notifyDataSetChanged();
        return b;

    }

    /**
     * 清空数据
     */
    public void clear() {
        contactList.clear();
    }

    @Override
    public int getItemCount() {
        return contactList.size();
    }

    @Override
    public void onBindViewHolder(ContactViewHolder contactViewHolder, final int i) {
        Life_Shopping item = contactList.get(i);
        contactViewHolder.name.setText(item.getName());
        contactViewHolder.price.setText(item.getPrice());
        contactViewHolder.spec.setText(item.getSpec());
        int getimages = UiUtils.getimages(item.getPhoto());
        Glide.with(MyApplication.getInstance()).load(getimages).into(contactViewHolder.image);
        contactViewHolder.im_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Life_Shopping item = (Life_Shopping) contactList.get(i);
                String name = item.getName();
                MyEvent eventBus = new MyEvent();
                eventBus.setType("add");
                eventBus.setName(name);
                eventBus.setPrice(item.getPrice());
                EventBus.getDefault().post(eventBus);
            }
        });
    }

    @Override
    public ContactViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View itemView = LayoutInflater.
                from(viewGroup.getContext()).
                inflate(R.layout.card_layout, viewGroup, false);

        return new ContactViewHolder(itemView);
    }

    public static class ContactViewHolder extends RecyclerView.ViewHolder {

        protected ImageView im_add, image;
        protected TextView name;
        protected TextView price;
        protected TextView spec;

        public ContactViewHolder(View v) {
            super(v);
            name = (TextView) v
                    .findViewById(R.id.name);
            price = (TextView) v
                    .findViewById(R.id.price2);
            spec = (TextView) v
                    .findViewById(R.id.spec);
            im_add = (ImageView) v
                    .findViewById(R.id.im_add);
            image = (ImageView) v
                    .findViewById(R.id.image);


        }
    }
}